package com.his.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.Application;
import com.his.entity.ApplyOut;
import com.his.entity.HisUser;
import com.his.mapper.ApplicationMapper;
import com.his.mapper.ApplyOutMapper;
import com.his.service.ApplyOutService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (ApplyOut)表服务实现类
 *
 * @author makejava
 * @since 2020-10-30 13:19:02
 */
@Service("applyOutService")
public class ApplyOutServiceImpl implements ApplyOutService {
    @Resource
    private ApplyOutMapper applyOutMapper;
    @Resource
    private ApplicationMapper applicationMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ApplyOut queryById(Integer id) {
        return this.applyOutMapper.queryById(id);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param pageParam 实例对象
     * @return 查询的结果
     */
    @Override
    public Rsp queryAll(PageParam<ApplyOut> pageParam, HisUser user) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        ApplyOut model = pageParam.getModel();
        if (user.getRoles().contains("nurse")) {
            model.setCheckUserId(user.getId());
        } else if (user.getRoles().contains("doctor")) {
            model.setCreateUserId(user.getId());
        }
        List<ApplyOut> list = this.applyOutMapper.queryAll(model);
        return Rsp.success(new PageInfo<>(list));
    }

    /**
     * 新增数据
     *
     * @param applyOut 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp insert(ApplyOut applyOut) {
        return this.applyOutMapper.insert(applyOut) > 0 ? Rsp.success("") : Rsp.fail().msg("修改失败");
    }

    /**
     * 修改数据
     *
     * @param applyOut 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp update(ApplyOut applyOut) {
        //修改出院时间
        Application application = new Application();
        application.setPatientId(applyOut.getPatientId());
        application.setOutTime(new Date());
        application.setApplyStatus("3");
        applicationMapper.out(application);
        return this.applyOutMapper.update(applyOut) > 0 ? Rsp.success("") : Rsp.fail().msg("修改失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.applyOutMapper.deleteById(id) > 0;
    }
}